﻿using System;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for ProviderCarMakeModelDB
/// </summary>
/// 
namespace CE.Provider
{
    public class CarMakeModelDB
    {
        public CarMakeModelDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(CarMakeModel providercarmakemodel)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarMakeModelInfoInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();
            #region Parameter

            _sPara.Add("@ProviderCarMakeModelId", providercarmakemodel.ProviderCarMakeModelId);
            _sPara.Add("@CarMakeId", providercarmakemodel.CarMakeId);
            _sPara.Add("@CarModelId", providercarmakemodel.CarModelId);
            _sPara.Add("@ProviderCompanyId", providercarmakemodel.ProviderCompanyId);
            _sPara.Add("@Description", providercarmakemodel.Description);
            _sPara.Add("@CarCode", providercarmakemodel.CarCode);
            _sPara.Add("@Body", providercarmakemodel.Body);
            _sPara.Add("@NumberOfDoors", providercarmakemodel.NumberOfDoors);
            _sPara.Add("@CC", providercarmakemodel.CC);
            _sPara.Add("@TurboEngine", providercarmakemodel.TurboEngine);
            _sPara.Add("@ParallelImport", providercarmakemodel.ParallelImport);
            _sPara.Add("@Rate", providercarmakemodel.Rate);
            _sPara.Add("@Agent", providercarmakemodel.Agent);
            _sPara.Add("@UW", providercarmakemodel.UW);
            _sPara.Add("@RefCEUserId", providercarmakemodel.RefCEUserId);
            _sPara.Add("@IsRefer", providercarmakemodel.IsRefer);
            _sPara.Add("@Status", providercarmakemodel.Status);
            _sPara.Add("@Finfo1", providercarmakemodel.FInfo1);
            _sPara.Add("@Dinfo1", providercarmakemodel.DInfo1);

            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public bool Delete(Int64 ProviderCarMakeModelId, Int64 ProviderCompanyId)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarMakeModelDeleteSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderCarMakeModelId", ProviderCarMakeModelId);
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result > 0;

        }

        public DataTable GetProviderCarMakeModelDT(Int64 ProviderCompanyId, int CarMakeId)
        {
            MyDatabase _db = new MyDatabase();
            DataTable _dtResult = new DataTable("Result");
            string _spName = "[sprocProviderCarMakeModelSelectItemByCarMakeId]";
            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);
            _sPara.Add("@CarMakeId", CarMakeId);
            #endregion

            _dtResult = _db.GetDataTableBySP(_spName, _sPara);
            return _dtResult;
        }

       
        public CarMakeModel GetProviderCarMakeModelItem(Int64 ProviderCompanyId,Int32 CarMakeId, Int32 CarModelId)
        {
            CarMakeModel _providercarmakemodel = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderCarMakeModelSelectSingleItem]";

            SortedList _sPara = new SortedList();
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);
            _sPara.Add("@CarMakeId", CarMakeId);
            _sPara.Add("@CarModelId", CarModelId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _sPara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _providercarmakemodel = new CarMakeModel();
                    _providercarmakemodel = FillDataRecord(_dt, 0);
                }
            }
            return _providercarmakemodel;
        }

        public CarMakeModel GetProviderCarMakeItemOnly(Int64 ProviderCompanyId, Int32 CarMakeId)
        {
            CarMakeModel _providercarmakemodel = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderCarMakeOnlySelectSingleItem]";

            SortedList _sPara = new SortedList();
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);
            _sPara.Add("@CarMakeId", CarMakeId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _sPara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _providercarmakemodel = new CarMakeModel();
                    _providercarmakemodel = FillDataRecord(_dt, 0);
                }
            }
            return _providercarmakemodel;
        }

        private CarMakeModel FillDataRecord(DataTable myDataTable, int rowId)
        {
            CarMakeModel myProviderCarMakeModel = new CarMakeModel();
            myProviderCarMakeModel.ProviderCarMakeModelId = Convert.ToInt64(myDataTable.Rows[rowId]["ProviderCarMakeModelId"].ToString());
            myProviderCarMakeModel.CarMakeId = Convert.ToInt32(myDataTable.Rows[rowId]["CarModelId"].ToString());
            myProviderCarMakeModel.CarModelId = Convert.ToInt32(myDataTable.Rows[rowId]["CarMakeId"].ToString());
            myProviderCarMakeModel.Description = myDataTable.Rows[rowId]["Description"].ToString();
            myProviderCarMakeModel.CarCode = myDataTable.Rows[rowId]["CarCode"].ToString();
            myProviderCarMakeModel.Body = myDataTable.Rows[rowId]["Body"].ToString();
            myProviderCarMakeModel.NumberOfDoors = Convert.ToInt32(myDataTable.Rows[rowId]["NumberOfDoors"].ToString());
            myProviderCarMakeModel.CC = Convert.ToInt32(myDataTable.Rows[rowId]["CC"].ToString());
            myProviderCarMakeModel.TurboEngine = Convert.ToBoolean(myDataTable.Rows[rowId]["TurboEngine"].ToString());
            myProviderCarMakeModel.ParallelImport = Convert.ToBoolean(myDataTable.Rows[rowId]["ParallelImport"].ToString());
            myProviderCarMakeModel.Rate = Convert.ToSingle(myDataTable.Rows[rowId]["Rate"].ToString());
            myProviderCarMakeModel.Agent = Convert.ToInt32(myDataTable.Rows[rowId]["Agent"].ToString());
            myProviderCarMakeModel.UW = Convert.ToInt32(myDataTable.Rows[rowId]["UW"].ToString());
            myProviderCarMakeModel.RefCEUserId = Convert.ToInt64(myDataTable.Rows[rowId]["RefCEUserId"].ToString());
            myProviderCarMakeModel.Status = Convert.ToBoolean(myDataTable.Rows[rowId]["Status"].ToString());
            myProviderCarMakeModel.CC2 = Convert.ToInt32(myDataTable.Rows[rowId]["CC2"].ToString());
            myProviderCarMakeModel.Body2 = myDataTable.Rows[rowId]["Body2"].ToString();

            if (myDataTable.Rows[rowId]["FInfo1"].ToString() != "")
                myProviderCarMakeModel.FInfo1 = Convert.ToSingle(myDataTable.Rows[rowId]["FInfo1"].ToString());

            if (myDataTable.Rows[rowId]["DInfo1"].ToString() != "")
                myProviderCarMakeModel.DInfo1 = Convert.ToDateTime(myDataTable.Rows[rowId]["DInfo1"].ToString());
            else
                myProviderCarMakeModel.DInfo1 = Convert.ToDateTime(myDataTable.Rows[rowId]["ModifiedDate"].ToString());

            return myProviderCarMakeModel;
        }
    }
}